<template>
	<view :class="isIphoneX ? 'iphone-x' : ''" :data-theme="themeStyle">
		<view class="cf-container ns-border-color-gray">
			<view class="tab">
				<view @click="changeState(1)"><text :class="state == 1 ? 'ns-text-color active ns-border-color-bottom' : ''">未使用</text></view>
				<view @click="changeState(2)"><text :class="state == 2 ? 'ns-text-color active ns-border-color-bottom' : ''">已使用</text></view>
				<view @click="changeState(3)"><text :class="state == 3 ? 'ns-text-color active ns-border-color-bottom' : ''">已过期</text></view>
			</view>
		</view>

		<!-- 180 -->
		<mescroll-uni ref="mescroll" top="100" @getData="getMemberCounponList">
			<block slot="list">
				<view class="coupon-list">
					<view class="coupon-li" v-for="(item, index) in list" :key="index" @click="toGoodsList(item)">
						<image :src="$util.img('upload/uniapp/coupon/coupon_ysy.png')" mode="widthFix"></image>
						<view class="li-top">
							<view class="top-content-left">
								<view class="coupon-name">{{ item.coupon_name }}</view>
								<view class="coupon-desc">
									<text v-if="item.validity_type">领取之日起{{ item.fixed_term }}日内有效</text>
									<text v-else>有效期至{{ $util.timeStampTurnTime(item.end_time) }}</text>
								</view>
							</view>
							<view class="top-content-right" v-if="item.type == 'reward'">
								<text class="top-content-right-fh ns-text-color" :class="{'top-content-right-active':item.state !== 1}">￥</text>
								<text class="font-big ns-text-color" :class="{'top-content-right-active':item.state !== 1}">{{ parseInt(item.money) }}</text>
							</view>
							<view class="top-content-right" v-if="item.type == 'discount'" :class="{'top-content-right-active':item.state !== 1}">
								<text class="font-big ns-text-color" :class="{'top-content-right-active':item.state !== 1}">{{ parseInt(item.discount) }}</text>
								<text class="font-sm ns-text-color" :class="{'top-content-right-active':item.state !== 1}">折</text>
							</view>
							<view class="tag" v-if="item.state == 2">已使用</view>
							<view class="tag" v-if="item.state == 3">已过期</view>
						</view>
						<view class="li-bottom">
							<view class="fonts-sm">
								<!-- <text v-if="item.goods_type==1">全场商品</text>
								<text v-if="item.goods_type!==1">指定商品</text> -->
								<text v-if="item.at_least > 0">满{{ item.at_least }}元可用</text>
								<text v-else>无门槛优惠券</text>
								<text class="fonts-sm-yuan ns-text-colorss" v-if="item.discount_limit != 0.00" :class="{'top-content-right-active':item.state !== 1}">(优惠上限{{item.discount_limit}}元)</text>
							</view>
							<view class="li-bottom-btn">
								<!-- <text class="to-use ns-text-color" v-if="item.goods_type != 1" @click="toGoodsList(item)" :class="{'top-content-right-active':item.state !== 1}">立即使用</text> -->
								<text class="to-use ns-text-color" v-if="item.state == 1" @click="toGoodsList(item)" :class="{'top-content-right-active':item.state !== 1}">立即使用</text>
							</view>
						</view>
					</view>
					<view v-if="!list.length && showEmpty" class="ns-margin-top cart-empty"  :fixed="!1"><ns-empty :emptyBtn="emptyBtn" text="暂无优惠券"></ns-empty></view>
				</view>
			</block>
		</mescroll-uni>
		<loading-cover ref="loadingCover"></loading-cover>
		<ns-show-toast></ns-show-toast>
	</view>
</template>

<script>
import globalConfig from '@/common/js/golbalConfig.js'	
import nsShowToast from '@/components/ns-show-toast/ns-show-toast.vue'
export default {
	data() {
		return {
			type: '',
			state: 1,
			list: [],
			isIphoneX: false, //判断手机是否是iphoneX以上
			token: '',
			showEmpty:false,
			emptyBtn:{
				text:"去领取",
				url: '/otherpages/goods/coupon/coupon'
			}
		};
	},
	components: {
		nsShowToast
	},
	mixins:[globalConfig],
	onLoad(data) {
		this.isIphoneX = this.$util.uniappIsIPhoneX();
	},
	onShow() {
		// 刷新多语言
		this.$langConfig.refresh();

		this.token = uni.getStorageSync('token');
		if (this.token == '') {
			this.$util.redirectTo('/pages/login/login/login');
			return;
		}
		if (this.$refs.mescroll) this.$refs.mescroll.refresh(false);
	},
	methods: {
		//切换状态
		changeState(state) {
			this.list = [];
			this.state = state;
			this.$refs.mescroll.refresh(false);
		},
		getMemberCounponList(mescroll) {
			this.showEmpty=false;
			this.$api.sendRequest({
				url: '/coupon/api/coupon/memberpage',
				data: {
					page: mescroll.num,
					page_size: mescroll.size,
					state: this.state,
					is_own: this.type
				},
				success: res => {
					this.showEmpty=true;
					let newArr = [];
					let msg = res.message;
					if (res.code == 0 && res.data) {
						newArr = res.data.list;
					} else {
						this.$util.showToast({
							title: msg
						});
					}
					mescroll.endSuccess(newArr.length);
					//设置列表数据
					if (mescroll.num == 1) this.list = []; //如果是第一页需手动制空列表
					this.list = this.list.concat(newArr); //追加新数据
					let data = res.data;
					if (data) this.couponList = data;
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail: res => {
					mescroll.endErr();
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},
		imageError(index) {
			this.list[index].logo = this.$util.getDefaultImage().default_shop_img;
			this.$forceUpdate();
		},
		toGoodsList(item){
			if(item.state == 1){
				this.$util.redirectTo('/pages/goods/list/list', {coupon: item.coupon_type_id})
			}
		}
	}
};
</script>

<style lang="scss" scoped>
	/deep/ .fixed{position: relative;top:0}
	.cart-empty{position:relative;top: 50%;margin-top: 104px !important;}
.top-content-right-active {
	color: #898989 !important;
}	
.coupon-list{
	width: 100%;
	height: 100%;
	padding: $ns-padding;
	box-sizing: border-box;

	.coupon-li{
		width: 702rpx;
		height: 210rpx;
		margin: 0 auto;
		border-radius: $ns-border-radius;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-bottom: $ns-margin;
		position: relative;
		overflow: hidden;
		
		image {
			width: 100%;
			height: 100%;
			position: absolute;
			left: 0;
			top: 0;
		}

		.li-top{
			width: 655rpx;
			margin: 0 auto;
			display: flex;
			justify-content: space-between;
			align-items: flex-end;
			border-bottom: 1px dashed #E1E1E1;
			position: relative;
			line-height: 1;
			margin-top: 34rpx;
			padding: 0 10rpx;
			box-sizing: border-box;
			padding-bottom: 34rpx;
			
			
			.top-content-left{
				display: flex;
				flex-direction: column;
				line-height: 1;
				flex:1;
				overflow: hidden;

				.coupon-name{
					color: #333;
					font-size: 28rpx;
					color: #000;
					line-height: 1;
					overflow:hidden;
					text-overflow:ellipsis;
					white-space:nowrap;
				}

				.coupon-desc{
					font-size: 20rpx;
					color: #ABABAB;
					line-height: 1;
					margin-top: 23rpx;
				}
			}

			.top-content-right{
				display: flex;
				justify-content: center;
				align-items: flex-end;
				line-height: 1;
				.font-sm{
					font-size: 30rpx;
					line-height: 1;
				}
				.font-big{
					font-size: 60rpx;
					line-height: 1;
				}
				.top-content-right-fh {
					font-size: 36rpx;
					font-weight: bold;
				}
			}
			.tag {
				width: 200rpx;
				height: 40rpx;
				font-size: 18rpx;
				color: #fff;
				background-color: #D0D0D0;
				text-align: center;
				line-height: 40rpx;
				position: absolute;
				right: -100rpx;
				top: -30rpx;
				transform: rotate(40deg);
			}
		}

		.li-bottom{
			flex: 1;
			width: 655rpx;
			margin: 0 auto;
			display: flex;
			justify-content: space-between;
			align-items: center;
			z-index: 999;
			padding: 0 10rpx;
			box-sizing: border-box;
			
			.fonts-sm{
				font-size: 22rpx;
				color: #ABABAB;
				
				.fonts-sm-yuan {
					color: #FF9493;
				}
			}
			
			.to-use{
				font-size: 24rpx;
				
				&:after{
					font-family: "iconfont" !important;
					content: '\e6a3';
					font-size: 24rpx;
				}
			}
		}
	}
}

.active {
	border-bottom: 4rpx solid;
}

.cf-container {
	background: #fff;
	overflow: hidden;
	border-bottom: 2rpx solid;
}

.tab {
	display: flex;
	justify-content: space-between;

	> view {
		text-align: center;
		width: 33%;

		text {
			display: inline-block;
			line-height: 88rpx;
		}
	}
}
</style>
